package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.Activity.SamplingActivity;
import com.tianxin.workbao.workBao.Activity.TestOptionActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Sampling;
import com.tianxin.workbao.workBao.dataProvide.SamplingProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class SamplingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Sampling> mSamplings;
    private Context mContext;
    private boolean isFromTest;

    public SamplingAdapter(List<Sampling> list, Context context, boolean from) {
        this.mSamplings = list;
        this.mContext = context;
        this.isFromTest = from;
    }

    @Override
    public int getItemViewType(int position) {
        if (mSamplings.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sampling_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mSamplings.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mSamplings.size() > 0) {
            return mSamplings.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mItemView;
        TextView samplingNumberTV;
        TextView addTimeTV;
        TextView carsTV;
        TextView netWeightTV;
        TextView statusLabelTV;
        TextView coalNameTV;
        TextView companyTV;
        TextView samplingUserTV;
        ImageButton deleteBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            mItemView = (LinearLayout) itemView.findViewById(R.id.item_view);
            samplingNumberTV = (TextView) itemView.findViewById(R.id.sampling_number);
            addTimeTV = (TextView) itemView.findViewById(R.id.add_time);
            carsTV = (TextView) itemView.findViewById(R.id.sampling_cars);
            netWeightTV = (TextView) itemView.findViewById(R.id.net_weight);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            coalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            companyTV = (TextView) itemView.findViewById(R.id.company);
            samplingUserTV = (TextView) itemView.findViewById(R.id.sampling_user);
            deleteBtn = (ImageButton) itemView.findViewById(R.id.delete_btn);
        }

        public void BindViewData(final Sampling sampling) {
            samplingNumberTV.setText(sampling.getSbnum());
            addTimeTV.setText(TimeUtils.parseDate(sampling.getAddtime()));
            carsTV.setText(sampling.getWeightscarnum() + " 车");
            netWeightTV.setText(sampling.getWeights() + " 吨");
            coalNameTV.setText(sampling.getCoaltype());
            companyTV.setText(sampling.getSupplycpname());
            samplingUserTV.setText(sampling.getAddusername());
            if (sampling.getIstest() == 1) {
                statusLabelTV.setText(mContext.getResources().getString(R.string.big_dot) + "已化");
                statusLabelTV.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
            } else {
                statusLabelTV.setText(mContext.getResources().getString(R.string.big_dot) + "未化");
                statusLabelTV.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_pressed_color));
            }
        }

        public void setViewListener(final int position) {
            if (isFromTest == true) {
                deleteBtn.setVisibility(View.GONE);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("sampling", mSamplings.get(position));
                        Intent intent = new Intent(mContext, TestOptionActivity.class);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                });
            } else {
                if (mSamplings.get(position).getIstest() == 1) {
                    deleteBtn.setVisibility(View.GONE);
                } else {
                    deleteBtn.setVisibility(View.VISIBLE);
                    deleteBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            SamplingProvide provide = new SamplingProvide(mContext, new UpdateListViewListener() {
                                @Override
                                public void onRefreshData(Object result) {

                                }

                                @Override
                                public void onLoadMoreData(Object result) {

                                }

                                @Override
                                public void showMessage(String msg) {
                                    if (msg == "ok") {
                                        mSamplings.remove(position);
                                        notifyDataSetChanged();
                                        Toast.makeText(mContext, "操作成功", Toast.LENGTH_LONG).show();
                                    } else if (msg.contains("failure")) {
                                        Toast.makeText(mContext, "操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                                    }
                                }

                                @Override
                                public void showProgress() {
                                    SamplingActivity activity = (SamplingActivity) mContext;
                                    activity.getSweetAlertDialog(activity).showDialog("",0);
                                }

                                @Override
                                public void hideProgress() {
                                    SamplingActivity activity = (SamplingActivity) mContext;
                                    activity.getSweetAlertDialog(activity).dismiss();
                                }
                            }, false);
                            //delete
                            provide.deleteSampling((int) mSamplings.get(position).getId());
                        }
                    });
                }
            }
        }
    }
}
